package org.will.demo.application;

import com.codahale.metrics.*;
import com.sun.javafx.font.Metrics;
import org.reveno.atp.api.Configuration;
import org.reveno.atp.api.Reveno;
import org.reveno.atp.core.Engine;
import org.reveno.atp.core.RevenoConfiguration;
import org.reveno.atp.core.storage.FileSystemStorage;
import org.will.common.ExtSnapshotStorage;
import org.will.common.RevenoHelper;
import org.will.common.metric.TestCounter;
import org.will.cqrs.command.Command;
import org.will.cqrs.command.CommandFactory;
import org.will.demo.commands.CreateMQDemoCommand;
import org.will.demo.commands.UpdateMQDemoCommand;
import org.will.demo.domain.MQDemo;
import org.will.demo.events.MQDemoCreateEvent;
import org.will.demo.events.MQDemoUpdateEvent;
import org.will.demo.view.MQDemoView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by EdifierWill on 2016/8/9.
 */
public class MQDemoService {

    Reveno _reveno = null;

    public MQDemoService()
    {
        init();
    }

    private void init()
    {
        _reveno = RevenoHelper.getInstance().getReveno();

        CommandFactory  commandFactory = new CommandFactory();
        List cmds = new ArrayList<Class>();
        cmds.add(CreateMQDemoCommand.class);
        cmds.add(UpdateMQDemoCommand.class);

        List actions = new ArrayList<Class>();
        actions.add(CreateMQDemoCommand.CreateMQDemoCommandAction.class);
        actions.add(UpdateMQDemoCommand.UpdateMQDemoCommandAction.class);

        List events = new ArrayList<Class>();
        events.add(MQDemoCreateEvent.class);
        events.add(MQDemoUpdateEvent.class);

        commandFactory.MakeCommand(cmds, actions, events);

        _reveno.domain().viewMapper(MQDemo.class, MQDemoView.class, (e, c, r) -> new MQDemoView(e, c._ename, c._price, c._inventory));
    }

    public void startup()
    {
        _reveno.startup();
    }

    public void shutdown()
    {
        _reveno.shutdown();
    }

    public void createMQDemo(Long id,String name,Double price,Double inventory)
    {
        TestCounter.getInstance().Mark("CreateMQDemo");
        _reveno.executeSync(new CreateMQDemoCommand(id,name,price,inventory));
    }

    public void updateMQDemo(Long id, String name,Double price,Double inventory)
    {
        _reveno.executeSync(new UpdateMQDemoCommand(id,name,price,inventory));
    }
}
